#!/bin/sh
#
# Author: Anthony Lee <don.anthony.lee+program@gmail.com>
# Purpose:
# 	Test all the testcases automatically in the skyeye testsuite.
#	On MinGW/CygWin/BeOS, there are none "expect".
#

TITLE="SkyEye Auto Test"

TEST_PATH="	at91/uclinux_rtl8019 \
		at91/uclinux_cs8900a \
		at91/at91_with_framebuffer_touchscreen \
		at91rm9200/2.6.x \
		ep7312/2.6.x \
		ep7312/ep7312_with_framebuffer_touchscreen_minigui \
		ep9312/2.6.x \
		s3c2410/2.4.18 \
		s3c4510 \
		pxa/2.6.x \
		pxa/with_flash_net_lcd pxa/pxa27x \
		cs89712 \
		strongarm \
		blackfin"

SKYEYE_SH="./exec_skyeye.sh"

if test "x$NO_DBCT" == "x"; then
	SKYEYE_SH="$SKYEYE_SH ./exec_skyeye_dbct.sh"
fi

# wait 5 minutes, sometimes it spends more time by this script.
TIMEOUT=300

CASE_NUM=1
FAILED_NUM=0
PASS_NUM=0
SKYEYE_RUNNING_STATE=0

REPORT_DATE=`date --iso-8601=date`
PWD=`dirname "$0"`
OUTPUT="${PWD}/test_report_${REPORT_DATE}"
TMP="${PWD}/tmp1_${REPORT_DATE}"
TIMEOUT_TMP="${PWD}/tmp2_${REPORT_DATE}"
TIMEOUT_REQUESTED_TMP="${PWD}/tmp3_${REPORT_DATE}"

# set title
set_terminal_title () {
	printf "\033]0;$1\007"
}

# pause
pause () {
	echo $1
	read
}

# puts
puts () {
	if test "x$2" == "x"; then
		echo "$1"
	else
		echo "$2"
		cat >> $1 <<_______EOF
$2
_______EOF
	fi
}

# kill the running skyeye
kill_skyeye () {
	if test "x$OSTYPE" == "xbeos"; then
		kill -9 skyeye > /dev/null 2>&1
	else
		PID=`ps auwx | grep skyeye | awk '{print $1}'`
		if test "x$PID" != "x"; then
			kill -9 $PID > /dev/null 2>&1
		fi
	fi
}

catch_pipe () {
	if read MESSAGE_FROM_PIPE; then
		echo "$MESSAGE_FROM_PIPE" >> "$TMP"

		IS_GET_SHELL=`echo "$MESSAGE_FROM_PIPE" | grep "shell"`

		if test "x$IS_GET_SHELL" != "x"; then
			# we think when appear "command shell"  , testcase is passed
			SKYEYE_RUNNING_STATE=1
		fi
	else
		# error
		SKYEYE_RUNNING_STATE=2
	fi
}

# run single test
run_test () {
	puts "$OUTPUT" " "
	puts "Running \"$1 $2\"..."

	rm -f "$TMP"

	if "$1" "$2" "$PWD" | "$0" --check; then
		puts "$OUTPUT" "Case $CASE_NUM: $1 $2 ...PASSED"
		PASS_NUM=`expr $PASS_NUM + 1`
	else
		puts "$OUTPUT" "Case $CASE_NUM: $1 $2 ...FAILED"
		FAILED_NUM=`expr $FAILED_NUM + 1`
		echo "####################################################" >> "$OUTPUT"
		if test -e "$TMP"; then
			cat "$TMP" >> "$OUTPUT"
		else
			echo "The file is inexistent" >> "$OUTPUT"
		fi
		echo "####################################################" >> "$OUTPUT"
	fi

	CASE_NUM=`expr $CASE_NUM + 1`
}

# run tests
run_tests () {
	for element in $TEST_PATH; do \
		run_test "$1" "$element"; \
	done
}


# main process

if test "x$OSTYPE" == "xbeos"; then
	if [ ! -t 1 ]; then
		# Launched from Tracker; open a shell window and run from there.
		export INWINDOW=1
		Terminal /bin/sh "$0"
		unset INWINDOW
		exit 0
	fi
fi

cd "$PWD"

if test "x$1" == "x--check"; then
	SKYEYE_CHECK_RETVAL=0

	rm -f "$TIMEOUT_TMP"
	rm -f "$TIMEOUT_REQUESTED_TMP"
	"$0" --timeout &

	while true; do
		catch_pipe
		if test $SKYEYE_RUNNING_STATE -ne 0; then
			if test $SKYEYE_RUNNING_STATE -ne 1; then
				SKYEYE_CHECK_RETVAL=1
			fi
			break
		fi
		if test -e "$TIMEOUT_TMP"; then
			SKYEYE_CHECK_RETVAL=1
			break
		fi
	done

	# wait for the timeout checking proccess to be quit
	if ! test -e "$TIMEOUT_TMP"; then
		echo " " > "$TIMEOUT_REQUESTED_TMP"
		while true; do
			if test -e "$TIMEOUT_TMP"; then
				break
			else
				sleep 1
			fi
		done
	fi

	rm -f "$TIMEOUT_TMP"
	rm -f "$TIMEOUT_REQUESTED_TMP"

	exit $SKYEYE_CHECK_RETVAL
elif test "x$1" == "x--timeout"; then
	ENDTIME=`date +%s`
	ENDTIME=`expr $ENDTIME + $TIMEOUT`

	while true; do
		sleep 2
		CURTIME=`date +%s`
		if test -e "$TIMEOUT_REQUESTED_TMP"; then
			# maybe something error while catch_pipe
			break
		fi
		if test $CURTIME -gt $ENDTIME; then
			# timeout
			break
		fi
	done

	kill_skyeye

	echo " " > "$TIMEOUT_TMP"

	exit 0
fi

set_terminal_title "$TITLE"

puts "*********************************"
puts "Start $TITLE"
puts "*********************************"

rm -f "$TMP"
rm -f "$OUTPUT"

puts "$OUTPUT" "                       SkyEye test report"
puts "$OUTPUT" "Date : `date -R`"
puts "$OUTPUT" "               "

for skyeye_element in $SKYEYE_SH; do \
	run_tests "$skyeye_element"; \
done

TOTAL_NUM=`expr $CASE_NUM - 1`

puts "$OUTPUT" " "
puts "$OUTPUT" "Total $TOTAL_NUM , failed $FAILED_NUM , pass $PASS_NUM "
puts "$TITLE ............"

rm -f "$TMP"
rm -f "$TIMEOUT_TMP"
rm -f "$TIMEOUT_REQUESTED_TMP"

echo.
pause "Press any key to quit ..."

